﻿using OurEDAFM.Models;
using Qiniu.Conf;
using Qiniu.IO;
using Qiniu.RS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OurEDAFM.Controllers
{
    public class UploadController : Controller
    {
        FMDBEntities db = new FMDBEntities();
        // GET: Upload

        public ActionResult AddSongs()
        {
            return View();
        }
        public string UploadData()
        {
            var filecollection = Request.Files;
            HttpPostedFileBase fileData = filecollection[0];
            Config.ACCESS_KEY = "yq2smDjfZktrXfIj_Ik86mHnQLLtE_-ZXbR7QSa_";
            Config.SECRET_KEY = "7RVwHeMgcx2FsC7IGmx4Hq7ACDPh5zTfR2GktEeE";
            string bucketName = "ouredafm-likesongs";
            PutPolicy put = new PutPolicy(bucketName);
            string uptoken = put.Token();
            PutExtra extra = new PutExtra();
            IOClient client = new IOClient();
            var ext = fileData.FileName.Substring(fileData.FileName.LastIndexOf('.') + 1).ToLower();
            var result = client.Put(uptoken, Guid.NewGuid().ToString() + "." + ext, fileData.InputStream, extra);
            if (result.OK == true)
            {
                return "http://ouredafm-likesongs.qiniudn.com/"+ result.key;
            }
            else
                return "null";
        }

        public ActionResult UpLikeSong(MyLikeSong newOne)
        {
            newOne.AddSong(newOne);
            return RedirectToAction("AddSongs");
        }
    }
}